package com.support.android.myapp3.acitivty;

import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v4.os.CancellationSignal;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hjq.toast.ToastUtils;
import com.support.android.myapp3.R;
import com.support.android.myapp3.customComponent.dialog.BaseDialog;
import com.support.android.myapp3.customComponent.dialog.CommonDialog;
import com.support.android.myapp3.customComponent.dialog.ConfirmDialog;
import com.support.android.myapp3.customComponent.dialog.ConfirmOldDialog;
import com.support.android.myapp3.customComponent.dialog.ViewConvertListener;
import com.support.android.myapp3.customComponent.dialog.ViewHolder;

import javax.crypto.Cipher;
import javax.crypto.Mac;

public class DialogActivity extends BaseActivity {


    @Override
    public int getLayoutView() {
        return R.layout.activity_dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    //确定样式一
    public void showConfirmClick1(View view) {
        ConfirmOldDialog.newInstance("1")
                .setMargin(60)
                .setOutCancel(false)
                .show(getSupportFragmentManager());
    }

    //确定样式一
    public void showConfirmClick(View view) {
        ConfirmDialog.newInstance().setTitle("通知1")
            .setMessage("通知111111111111111111111111")
                .setCancelClickListener(new ConfirmDialog.ClickListener() {
                    @Override
                    public void onClick(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                })
                .setConfirmClickListener(new ConfirmDialog.ClickListener() {
                    @Override
                    public void onClick(BaseDialog dialog) {
                        dialog.dismiss();
                        ToastUtils.show("111111111111111");
                    }
                }).setMargin(60)
                .setOutCancel(false)
                .show(getSupportFragmentManager());
    }

    //确定样式一
    public void showConfirmClick2(View view) {
        CommonDialog.newInstance()
                .setLayoutId(R.layout.dialog_confirm)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseDialog dialog) {
                        holder.setText(R.id.title,"通知");
                        holder.setText(R.id.message,"通知通知通知通知通知通知通知!");
                        holder.setOnClickListener(R.id.cancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(DialogActivity.this, "选择了取消", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        });
                        holder.setOnClickListener(R.id.confirm, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(DialogActivity.this, "选择了确认", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        });
                    }
                })
                .setMargin(60)
                .setOutCancel(false)
                .show(getSupportFragmentManager());
    }

    //确定样式二
    public void showConfirm2Click(View view) {
        ConfirmOldDialog.newInstance("2")
                .setMargin(60)
                .setOutCancel(true)
                .show(getSupportFragmentManager());
    }

    //分享样式
    public void showShareClick(View view) {
        CommonDialog.newInstance()
                .setLayoutId(R.layout.dialog_share)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseDialog dialog) {
                        holder.setOnClickListener(R.id.tv_weixin_share, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(DialogActivity.this, "微信分享", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        });
                    }
                })
                .setDimAmout(0.3f)
                .setShowBottom(true)
                .setAnimStyle(R.style.EnterExitAnimation)
                .show(getSupportFragmentManager());
    }

    //好有设置
    public void showFriendClick(View view) {
        CommonDialog.newInstance()
                .setLayoutId(R.layout.dialog_friend)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseDialog dialog) {
                    }
                })
                .setShowBottom(true)
                .setSize(0, 310)
                .show(getSupportFragmentManager());
    }

    //评论样式
    public void showCommentClick(View view) {
        CommonDialog.newInstance()
                .setLayoutId(R.layout.dialog_comment)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseDialog dialog) {
                        final EditText et_input = holder.getView(R.id.edit_input);

                        holder.setOnClickListener(R.id.tv_commit, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String text = et_input.getText().toString().trim();
                                text = TextUtils.isEmpty(text) ? "请输入文字" : et_input.getText().toString().trim();
                                Toast.makeText(DialogActivity.this, text, Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        });
                    }
                })
                .setShowBottom(true)
                .show(getSupportFragmentManager());
    }

    //指纹样式
    public void showFingerClick(View view) {
        CommonDialog.newInstance()
                .setLayoutId(R.layout.dialog_finger)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseDialog dialog) {
                        FingerprintManagerCompat manager;
                        final CancellationSignal mCancellationSignal = new CancellationSignal();




                        holder.setText(R.id.message,"正在进行指纹验证");
                        holder.setOnClickListener(R.id.cancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mCancellationSignal.cancel();
                                dialog.dismiss();
                                Toast.makeText(DialogActivity.this, "cannel", Toast.LENGTH_SHORT).show();

                            }
                        });

                        manager = FingerprintManagerCompat.from(getBaseContext());
                        if(!manager.hasEnrolledFingerprints()){
                            ToastUtils.show("请至少录入一个指纹");
                            return;
                        }

                        manager.authenticate(null, 0, mCancellationSignal, new MyCallBack(holder,dialog), null);


                    }
                })
                .setMargin(80)
                .setOutCancel(false)
                .setDimAmout(0.3f)
                .setAnimStyle(R.style.EnterExitAnimation)
                .show(getSupportFragmentManager());

    }

    public class MyCallBack extends FingerprintManagerCompat.AuthenticationCallback {
        private static final String TAG = "MyCallBack";
        BaseDialog dialog;
        ViewHolder  holder;
        public MyCallBack(ViewHolder holder, final BaseDialog dialog){
            this.holder = holder;
            this.dialog = dialog;
        }

        // 当出现错误的时候回调此函数，比如多次尝试都失败了的时候，errString是错误信息
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            Log.d(TAG, "onAuthenticationError: " + errString);
            ToastUtils.show("onAuthenticationError");
        }

        // 当指纹验证失败的时候会回调此函数，失败之后允许多次尝试，失败次数过多会停止响应一段时间然后再停止sensor的工作
        @Override
        public void onAuthenticationFailed() {
            Log.d(TAG, "onAuthenticationFailed: " + "验证失败");
            ToastUtils.show("onAuthenticationFailed: " + "验证失败");
            holder.setText(R.id.message,"指纹验证失败");
        }

        @Override
        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
            Log.d(TAG, "onAuthenticationHelp: " + helpString);
            ToastUtils.show("onAuthenticationHelp: " + helpString);
        }

        // 当验证的指纹成功时会回调此函数，然后不再监听指纹sensor
        @Override
        public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult
                                                      result) {
            Log.d(TAG, "onAuthenticationSucceeded: " + "验证成功");

            holder.setText(R.id.message,"指纹验证成功");
            dialog.dismiss();
            ToastUtils.show("onAuthenticationSucceeded: " + "验证成功");
        }
    }
}
